//
//  ALPS_TListDetailViewController.m
//  Alps
//
//  Created by dllo on 16/6/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_TListDetailViewController.h"
#import "WSSNetTool.h"
#import "ALPS_TListDModel.h"
#import "ALPS_TListDTCell.h"
#import "ALPS_TSearchViewController.h"
#import "ALPS_ClassifyClickClickAlbumController.h"

@interface ALPS_TListDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *leftTableView;

@property (nonatomic, strong) UITableView *rightTableView;
/** 左边数组 */
@property (nonatomic, strong) NSMutableArray *leftData;
/** 右边数组 */
@property (nonatomic, strong) NSMutableArray *rightData;

/** 为了让左边数组只走一次 */
@property (nonatomic, assign) NSInteger temp;

//加载页面
@property (nonatomic, assign) NSInteger page;
//判断是否是总榜加载
@property (nonatomic, assign) NSInteger judge;
//记录非总榜加载的key
@property (nonatomic, copy) NSString *content;
@end

@implementation ALPS_TListDetailViewController

- (void)dealloc
{
    _leftTableView.dataSource = nil;
    _leftTableView.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.leftData = [NSMutableArray array];
    self.rightData = [NSMutableArray array];
    self.page = 1;
    self.temp = 0;
    [self getLeftTableView];
    [self getRighttableView];
    [self setLeftBackButton];
    [self getLeftdata:self.webKey type:self.typeJudgment number:1];

}


-(void)getLeftdata:(NSString *)string type:(NSString *)contentType number:(NSInteger)num
{
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/%@?device=iPhone&key=%@&pageId=%ld&pageSize=20&scale=3", contentType, string, num] body:nil headers:nil response:0 success:^(id result) {
        NSArray *arr = [result objectForKey:@"categories"];
        NSArray *array = [result objectForKey:@"list"];
        for (NSDictionary *dict in array) {
            ALPS_TListDModel *model = [[ALPS_TListDModel alloc] initWithDic:dict];
            [self.rightData addObject:model];
        }
        
        if (self.temp == 0) {
            for (NSDictionary *dic in arr) {
                ALPS_TListDModel *model = [[ALPS_TListDModel alloc] initWithDic:dic];
                [self.leftData addObject:model];
            }
            [self.leftTableView reloadData];
            self.temp++;
        }

        [self.rightTableView.mj_header endRefreshing];
        [self.rightTableView.mj_footer endRefreshing];
        if (self.page == [[result objectForKey:@"maxPageId"] integerValue]) {
            [self.rightTableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.rightTableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

//右边界面
-(void)getRighttableView
{
    self.rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(ScreeWidth * 0.28, 0, ScreeWidth - ScreeWidth * 0.28, ScreeHeight - 64) style:UITableViewStylePlain];
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;
    self.rightTableView.separatorStyle = UITableViewStylePlain;
    [self.view addSubview:_rightTableView];
    [self.rightTableView registerClass:[ALPS_TListDTCell class] forCellReuseIdentifier:@"ALPS_TListDTCell"];
    [self.rightTableView registerClass:[ALPS_TListDTCell class] forCellReuseIdentifier:@"ALPS_TListDTCell1"];
    [self.rightTableView registerClass:[ALPS_TListDTCell class] forCellReuseIdentifier:@"ALPS_TListDTCell2"];
    [self.rightTableView registerClass:[ALPS_TListDTCell class] forCellReuseIdentifier:@"ALPS_TListDTCell3"];
    self.rightTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreshData];
    }];
    self.rightTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadingData];
    }];
    
}
//左边界面
-(void)getLeftTableView
{
    self.leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth * 0.28, ScreeHeight - 64) style:UITableViewStylePlain];
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    self.leftTableView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_leftTableView];
    [self.leftTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (tableView == self.leftTableView) {
        //总榜以后的
        if (indexPath.row > 0) {
        if (self.leftData.count > 0) {
                ALPS_TListDModel *model = [self.leftData objectAtIndex:indexPath.row - 1];
                NSString *str = model.key;
                [self.rightData removeAllObjects];
                [self.rightTableView reloadData];
                [self getLeftdata:str type:self.typeJudgment number:1];
                 self.page = 1;
            self.judge = 1;
            self.content = str;
            }
        }
        //总榜
        else if(indexPath.row == 0)
        {
            [self.rightData removeAllObjects];
            [self.rightTableView reloadData];
            [self getLeftdata:self.webKey type:self.typeJudgment number:1];
            self.page = 1;
            self.judge = 0;
        }
   }
    //右边点击方法
    if (tableView == self.rightTableView) {
        if ([self.typeJudgment isEqualToString:@"album"]) {
            ALPS_ClassifyClickClickAlbumController *ciyVC = [[ALPS_ClassifyClickClickAlbumController alloc] init];
            ALPS_TListDModel *dataModel = [self.rightData objectAtIndex:indexPath.row];
            NSLog(@"%ld", dataModel.albumId);
            ciyVC.albumId = dataModel.albumId;
            [self.navigationController pushViewController:ciyVC animated:YES];
        }
        else
        {
            ALPS_PlayerMainViewController *pmVC = [[ALPS_PlayerMainViewController alloc] init];
            ALPS_TListDModel *dataModel = [self.rightData objectAtIndex:indexPath.row];
            pmVC.trackId = dataModel.trackId;
            pmVC.listArr = self.rightData;
            [self.navigationController pushViewController:pmVC animated:YES];
        }
        
    }

}

//刷新
-(void)refreshData
{
    if (self.judge == 1) {
        [self.rightData removeAllObjects];
        [self.rightTableView reloadData];
        [self getLeftdata:self.content type:self.typeJudgment number:1];
        self.page = 1;

    }else
    {
        [self.rightData removeAllObjects];
        [self.rightTableView reloadData];
        [self getLeftdata:self.webKey type:self.typeJudgment number:1];
        self.page = 1;
    }

    
}

//加载
-(void)loadingData
{
    if (self.judge == 1) {
        self.page++;
        [self getLeftdata:self.content type:self.typeJudgment number:self.page];
    }else
    {
    self.page++;
    [self getLeftdata:self.webKey type:self.typeJudgment number:self.page];
    }
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.rightTableView) {
        return 100;
    }
    return  44;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.leftTableView) {
        
        return self.leftData.count + 1;
    }
    return self.rightData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.leftTableView) {
        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            cell.textLabel.text = @"总榜";
            cell.textLabel.font = [UIFont systemFontOfSize:13];
            return cell;
        }
        else
        {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            ALPS_TListDModel *model = [self.leftData objectAtIndex:indexPath.row - 1];
            cell.textLabel.font = [UIFont systemFontOfSize:13];
            cell.textLabel.text = model.name;
            return cell;
        }
    }
    else if(tableView == self.rightTableView)
    {
        ALPS_TListDModel *model = [self.rightData objectAtIndex:indexPath.row];
        
        //为了让前三名变色
        ALPS_TListDTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ALPS_TListDTCell"];
        cell.mdoel = [self.rightData objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellStyleDefault;
        cell.rankLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        
        ALPS_TListDTCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"ALPS_TListDTCell1"];
        cell1.mdoel = [self.rightData objectAtIndex:indexPath.row];
        cell1.selectionStyle = UITableViewCellStyleDefault;
        cell1.rankLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        
        
        ALPS_TListDTCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"ALPS_TListDTCell2"];
        cell2.mdoel = [self.rightData objectAtIndex:indexPath.row];
        cell2.selectionStyle = UITableViewCellStyleDefault;
        cell2.rankLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];

        
        ALPS_TListDTCell *cell3 = [tableView dequeueReusableCellWithIdentifier:@"ALPS_TListDTCell3"];
        cell3.mdoel = [self.rightData objectAtIndex:indexPath.row];
        cell3.selectionStyle = UITableViewCellStyleDefault;
        cell3.rankLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];        
        
        if ([self.typeJudgment isEqualToString:@"album"]) {
            cell.bluesLabel.text = [NSString stringWithFormat:@"%ld集", model.tracksCounts];
            cell.albumView.image = [UIImage imageNamed:@"album_tracks"];
        }
        switch (indexPath.row + 1) {
            case 1:
                cell1.rankLabel.textColor = [UIColor redColor];
                if ([self.typeJudgment isEqualToString:@"album"]) {
                    cell1.bluesLabel.text = [NSString stringWithFormat:@"%ld集", model.tracksCounts];
                    cell1.albumView.image = [UIImage imageNamed:@"album_tracks"];
                }

                return cell1;
                break;
             case 2:
                cell2.rankLabel.textColor = [UIColor orangeColor];
                if ([self.typeJudgment isEqualToString:@"album"]) {
                    cell2.bluesLabel.text = [NSString stringWithFormat:@"%ld集", model.tracksCounts];
                    cell2.albumView.image = [UIImage imageNamed:@"album_tracks"];
                }

                return cell2;
                break;
                case 3:
                cell3.rankLabel.textColor = [UIColor colorWithRed:0.42 green:0.69 blue:0.22 alpha:1.00];
                if ([self.typeJudgment isEqualToString:@"album"]) {
                    cell3.bluesLabel.text = [NSString stringWithFormat:@"%ld集", model.tracksCounts];
                    cell3.albumView.image = [UIImage imageNamed:@"album_tracks"];
                }

                return cell3;
                break;
            default:
                
                break;
        }
            return cell;
    }
    return nil;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
